﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SimpleCodeGen
{
    /// <summary>
    ///  Business object generation
    /// </summary>
    public static class BusinessObjectGen
    {
        /// <summary>
        /// Generates the business object.
        /// </summary>
        public static void GenerateBusinessObject(IEnumerable<CustomColumn> columns)
        {
            if (columns != null && columns.Count() > 0)
            {
                var builder = new StringBuilder();
                builder.AppendLine("// --------------------------------------------------------------------------------------------------------------------");
                builder.AppendLine("// <copyright file=");
                builder.AppendLine("//   Copyright 2011 <#= companyName #>");
                builder.AppendLine("// </copyright>");
                builder.AppendLine("// <summary>");
                builder.AppendLine("// <#= className #> Class Definition");
                builder.AppendLine("// </summary>");
                builder.AppendLine("// <author><#= developer #> </author>");
                builder.AppendLine("// <history><#= currentDate #> </history>");
                builder.AppendLine("// --------------------------------------------------------------------------------------------------------------------");

                builder.AppendLine("namespace  <#= BONameSpace #>{");
                builder.AppendLine("#region usings");
                builder.AppendLine("using System;");
                builder.AppendLine("#endregion");
                builder.AppendLine("/// <summary>");
                builder.AppendLine("///  <#= className #> ");
                builder.AppendLine("/// </summary>");
                builder.AppendLine("public class <#= className #> ");
                builder.AppendLine("{");

                foreach (var customColumn in columns)
                {
                    builder.AppendLine("/// <summary>");
                    builder.AppendLine("/// Gets or sets the " + customColumn.ColumnName);
                    builder.AppendLine("/// </summary>");
                    builder.AppendLine("/// <value>");
                    builder.AppendLine("///" + customColumn.ColumnName);
                    builder.AppendLine("/// </value>");
                    builder.AppendLine(" public " + GetDataType(customColumn) + " " + customColumn.ColumnName + " { get; set;}");
                    builder.AppendLine(" ");
                    builder.AppendLine(" ");
                }
                builder.AppendLine("}");
                builder.AppendLine("}");

                // write to file
                using (var outfile = new StreamWriter(@"c:\temp\simplecodegen.cs"))
                {
                    outfile.Write(builder.ToString());
                }
            }
        }

        /// <summary>
        /// Gets the type of the data.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        private static string GetDataType(CustomColumn column)
        {
            var result = string.Empty;
            if (column != null && !string.IsNullOrEmpty(column.DataType))
            {
                if (column.DataType.ToLower().Contains("string") || column.DataType.ToLower().Contains("byte"))
                {
                    result = column.DataType;
                }
                else
                {
                    result = string.Format("{0}?", column.DataType);
                }
            }

            return result;
        }
    }
}